﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;
using eTMS.Controllers.HoSoService;
using eTMS.Controllers.LoaiGiayToService;

namespace eTMS.Forms.Students
{
    public partial class FrmGiayToSinhVien : eTMS.Controls.BaseForm
    {
        public FrmGiayToSinhVien()
        {
            InitializeComponent();
        }
        SinhVienController sinhVienController = new SinhVienController();
        GiayToSinhVienController giayToSinhVienController = new GiayToSinhVienController();
        HoSoController hoSoController = new HoSoController();
        private void DoSearchSinhVien()
        {
            if (sinhVienController == null) return;
            sinhVienController.ResetObject();
            sinhVienInfoInput1.UpdateToModel();
            sinhVienController.SearchSinhVien(treeKhoaNganhKhoaHoc1.SelectedKhoa_ID, treeKhoaNganhKhoaHoc1.SelectedNganhHoc_ID, treeKhoaNganhKhoaHoc1.SelectedLop_ID, treeKhoaNganhKhoaHoc1.KhoaHoc_ID);
            sinhVienList1.UpdateToScreen();
        }
        private void FrmGiayToSinhVien_Load(object sender, EventArgs e)
        {
            treeKhoaNganhKhoaHoc1.InitData();
            HoSoController.LoadAllHoSo(false);
            LoaiGiayToController.LoadAllLoaiGiayTo(false);
            comboBoxHoSo1.BindData();
            sinhVienList1.SetController(sinhVienController);
            sinhVienInfoInput1.SetController(sinhVienController);
        }

        private void treeKhoaNganhKhoaHoc1_OnChangeTreeSelected(object sender, EventArgs e)
        {
            DoSearchSinhVien();
        }
        private void LoadLoaiGiayTo()
        {
            txtLoaiGiayTo.DisplayMember = "TenLoaiGiayTo";
            txtLoaiGiayTo.ValueMember = "LoaiGiayTo_ID";
            List<LoaiGiayToEntity> lsLoaiGiayTo = new List<LoaiGiayToEntity>();
            LoaiGiayToEntity oAll = new LoaiGiayToEntity();
            oAll.MaLoaiGiayTo = eTMS.Commons.Constants.APP_ALL;
            oAll.TenLoaiGiayTo = eTMS.Commons.Constants.APP_ALL;
            oAll.LoaiGiayTo_ID = 0;
            lsLoaiGiayTo.Add(oAll);
            if (comboBoxHoSo1.SelectedIndex > 0)
            {
                hoSoController.HoSoObject = (HoSoEntity)comboBoxHoSo1.SelectedItem;
                hoSoController.LoadDataChiTiet();
                foreach (LoaiGiayToEntity o in LoaiGiayToController.ListLoaiGiayToStatic)
                {
                    if (hoSoController.tblDataDetail.Select("LoaiGiayTo_ID=" + o.LoaiGiayTo_ID.ToString()).Length > 0)
                    {
                        lsLoaiGiayTo.Add(o);
                    }
                }
            }
            else
            {
                lsLoaiGiayTo.AddRange(LoaiGiayToController.ListLoaiGiayToStatic);
            }
            txtLoaiGiayTo.DataSource = lsLoaiGiayTo;
        }
        private void comboBoxHoSo1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBoxHoSo1.SelectedIndex < 0) return;
            LoadLoaiGiayTo();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            List<int> lsID = new List<int>();
            if (txtLoaiGiayTo.SelectedIndex > 0)
            {
                lsID.Add((int)txtLoaiGiayTo.SelectedValue);
            }
            else
            {
                for (int i = 1; i < txtLoaiGiayTo.Items.Count; i++)
                {
                    lsID.Add(((LoaiGiayToEntity)txtLoaiGiayTo.Items[i]).LoaiGiayTo_ID);
                }
            }
            try
            {
                giayToSinhVienController.SaveGiayToSinhVien(lsID, (int)txtSoLuong.Value, sinhVienList1.GetSelectedSinhVienRows());
                ShowInfo(eTMS.Commons.Constants.MSS_SAVE_SUCCESS);
            }
            catch
            {
                ShowInfo(eTMS.Commons.Constants.MSS_SAVE_FAILE);
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            List<int> lsID = new List<int>();
            if (txtLoaiGiayTo.SelectedIndex > 0)
            {
                lsID.Add((int)txtLoaiGiayTo.SelectedValue);
            }
            else
            {
                for (int i = 1; i < txtLoaiGiayTo.Items.Count; i++)
                {
                    lsID.Add(((LoaiGiayToEntity)txtLoaiGiayTo.Items[i]).LoaiGiayTo_ID);
                }
            }
            if ((lsID.Count > 0))
            {
                if (Confirm("Bạn thực sự muốn xóa giấy tờ chọn cho các sinh viên không?"))
                {
                    giayToSinhVienController.RemoveGiayToSinhVien(lsID, (int)txtSoLuong.Value, sinhVienList1.GetSelectedSinhVienRows());
                   
                }
            }
        }

        private void sinhVienList1_OnSelectionChanged(object sender, EventArgs e)
        {
            sinhVienList1.UpdateObjectToModel();
            if (sinhVienController.SinhVienObject != null)
            {
                giayToSinhVienController.LoadAllDSGiayToSinhVien(sinhVienController.SinhVienObject.SinhVien_ID);
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = giayToSinhVienController.Table;
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                giayToSinhVienController.UpdateGiayToSinhVien();
                ShowInfo(eTMS.Commons.Constants.MSS_SAVE_SUCCESS);
            }
            catch
            {
                ShowInfo(eTMS.Commons.Constants.MSS_SAVE_FAILE);
            }
            
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            DoSearchSinhVien();
        }

        private void btnTraGiayTo_Click(object sender, EventArgs e)
        {
            giayToSinhVienController.TraGiayToHienTai();
            ShowInfo("Bạn đã trả giấy tờ thành công!");
        }

        private void btnTraTat_Click(object sender, EventArgs e)
        {
             List<int> lsID = new List<int>();
            if (txtLoaiGiayTo.SelectedIndex > 0)
            {
                lsID.Add((int)txtLoaiGiayTo.SelectedValue);
            }
            else
            {
                for (int i = 1; i < txtLoaiGiayTo.Items.Count; i++)
                {
                    lsID.Add(((LoaiGiayToEntity)txtLoaiGiayTo.Items[i]).LoaiGiayTo_ID);
                }
            }
            if ((lsID.Count > 0))
            {
                if (Confirm("Bạn thực sự muốn trả giấy tờ chọn cho các sinh viên không?"))
                {
                    giayToSinhVienController.TraGiayToByListSinhVien(lsID, sinhVienList1.GetSelectedSinhVienRows());
                }
            }
        }

    }
}
